package com.nowcoder.code.lcof;

public class JZ53 {

    public static void main(String[] args) {
        System.out.println(new JZ53().GetNumberOfK(new int[]{3,3,3,3}, 4));
    }


    public int GetNumberOfK(int[] nums, int k) {

        if (nums == null || nums.length == 0) {
            return 0;
        }


        int start = 0;
        int end = nums.length - 1;
        int middle = end >> 1;

        while (start < end) {
            if (nums[middle] == k) {
                break;
            }

            if (nums[middle] > k) {
                end = middle - 1;
            } else {
                start = middle + 1;
            }
            middle = (end + start) >> 1;


        }
        if (nums[middle] != k) {
            return 0;
        }

        while (middle < nums.length && nums[middle] == k) {
            middle++;
        }
        middle--;
        int sum = 0;
        while (middle >= 0 && nums[middle] == k) {
            sum++;
            middle--;
        }
        return sum;

    }

}
